package com.doodod.common.dao.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * User: 邱孝兵
 * Date: 14-2-28
 * Time: 上午4:31
 * To change this template use File | Settings | File Templates.
 * 用于存储分页结果
 */

public class PageBean<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    //当前页数
    private int pageNo;
    //下一页
    private int nextNo;
    //上一页
    private int priorNo;
    //总页数
    private int pageCount;
    //当前页的记录个数
    private int rowCount;
    //一页的记录数目
    private int pageSize;
    //当前页的起始记录
    private int startRow;
    //排序条件
    private String orderBy;
    //排序类型
    private String orderType;
    //页码列表
    private List<String> pageList;
    //默认显示页码个数
    private int pageListSize = 10;
    //当前记录的列表
    private List<T> list;

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }

    public void setNextNo(int nextNo) {
        this.nextNo = nextNo;
    }

    public void setPriorNo(int priorNo) {
        this.priorNo = priorNo;
    }

    public void setPageList(List<String> pageList) {
        this.pageList = pageList;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    private String groupby;

    public PageBean() {
        pageSize = 10;
        pageNo = 1;
        startRow = 0;
        rowCount = 0;
    }

    public int getNextNo() {
        nextNo = this.getPageNo() + 1;
        if (nextNo > pageCount)
            return pageCount;
        return nextNo;
    }

    public int getPriorNo() {
        priorNo = this.getPageNo() - 1;
        if (priorNo < 1)
            return 1;
        return priorNo;
    }

    public int getPageCount(int rowCounts) {
        if (rowCounts % pageSize == 0 && rowCounts > pageSize) {
            pageCount = rowCounts / pageSize;
        } else if (rowCounts % pageSize != 0 && rowCounts > pageSize) {
            pageCount = rowCounts / pageSize + 1;

        } else {
            pageCount = 1;
        }
        return pageCount;
    }

    public PageBean(int pageNo, int pageSize) {
        this.pageSize = pageSize;
        this.pageNo = pageNo;
    }

    public int getPageNo() {
        if (pageNo <= 0) {
            pageNo = 1;
        }
        return pageNo;
    }

    public void setPageNo(Integer pageNo) {
        if (pageNo == null)
            pageNo = 0;
        this.pageNo = pageNo;
    }

    public int getPageCount() {
        if (rowCount % pageSize == 0 && rowCount > pageSize) {
            pageCount = rowCount / pageSize;
        } else if (rowCount % pageSize != 0 && rowCount > pageSize) {
            pageCount = rowCount / pageSize + 1;
        } else {
            pageCount = 1;
        }
        return pageCount;
    }

    public void setPageCount(int pageCount) {
        this.pageCount = pageCount;
    }

    public int getRowCount() {
        return rowCount;
    }

    public void setRowCount(int rowCount) {
        this.rowCount = rowCount;
        if (pageSize <= 0) {
            pageSize = 10;
        }
        pageCount = (rowCount + pageSize - 1) / pageSize;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        if (pageSize > 100)
            this.pageSize = 100;
        else
            this.pageSize = pageSize;
    }

    public int getStartRow() {
        startRow = (pageNo - 1) * pageSize;
        if (startRow < 0) {
            startRow = 0;
        }
        return startRow;
    }

    public void setStartRow(int startRow) {
        this.startRow = startRow;
    }

    public List<String> getPageList() {
        if (pageCount > 0) {
            pageList = new ArrayList<String>();

            int startNo = 1;
            int endNo = 0;
            if (pageNo - pageListSize / 2 > 0) {
                startNo = pageNo - pageListSize / 2;
            }
            if (pageNo + pageListSize / 2 < pageCount) {
                endNo = pageNo + pageListSize / 2;
            } else {
                endNo = pageCount;
            }
            for (int i = startNo; i <= endNo; i++) {
                pageList.add(Integer.toString(i));
            }
            return pageList;
        } else {
            return new ArrayList<String>();
        }
    }

    public int getPageListSize() {
        return pageListSize;
    }

    public void setPageListSize(int pageListSize) {
        this.pageListSize = pageListSize;
    }

    public List<T> getList() {
        return list;
    }

    public String getOrderBy() {
        return orderBy;
    }

    public void setOrderBy(String orderBy) {
        this.orderBy = orderBy;
    }

    public String getOrderType() {
        return orderType;
    }

    public void setOrderType(String orderType) {
        this.orderType = orderType;
    }

    public String getGroupby() {
        return groupby;
    }

    public void setGroupby(String groupby) {
        this.groupby = groupby;
    }

    @Override
    public String toString() {
        return "PageBean [pageNo=" + pageNo + ", nextNo=" + nextNo
                + ", priorNo=" + priorNo + ", pageCount=" + pageCount
                + ", rowCount=" + rowCount + ", pageSize=" + pageSize
                + ", startRow=" + startRow + ", orderBy=" + orderBy
                + ", orderType=" + orderType + ", pageList=" + pageList
                + ", pageListSize=" + pageListSize + ", list=" + list
                + ", groupby=" + groupby + "]";
    }
}
